<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="区域名称" prop="zoneName">
        <el-input
            v-model="queryParams.zoneName"
            placeholder="请输入场馆区域名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域编号" prop="zoneCode">
        <el-input
            v-model="queryParams.zoneCode"
            placeholder="请输入场馆区域编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width:150px">
          <el-option
              v-for="dict in junti_venue_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="训练分类" prop="trainType">
        <el-select v-model="queryParams.trainType" placeholder="请选择分类" clearable style="width:150px">
          <el-option
              v-for="dict in junti_train_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['base:venueZone:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['base:venueZone:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="venueZoneList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="序号" type="index" width="70" align="center" fixed>
        <template #default="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="区域名称" align="center" prop="zoneName" />
      <el-table-column label="区域编号" align="center" prop="zoneCode" />
      <el-table-column label="面积(平方米)" align="center" prop="zoneSize" />
      <el-table-column label="建成日期" align="center" prop="buildDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.buildDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="junti_venue_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="开放时间" align="center" prop="startTimeSpan" >
        <template #default="scope">
          <span>{{scope.row.startTimeSpan}} - {{scope.row.endTimeSpan}} </span>
        </template>
      </el-table-column>
      <el-table-column label="管理人员" align="center" prop="manager" />
      <el-table-column label="手机号" align="center" prop="phonenumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['base:venueZone:edit']">修改</el-button>
          <el-button link type="primary" icon="Setting" @click="handleDeviceManagement(scope.row)" v-hasPermi="['base:venueDevice:manage']">设备管理</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['base:venueZone:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改场馆区域对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" v-model="open" width="55%" append-to-body>
      <el-form ref="venueZoneRef" :model="form" :rules="rules" label-width="120px">
        <el-row>

          <el-col :span="12">
            <el-form-item label="场馆区域名称" prop="zoneName">
              <el-input v-model="form.zoneName" placeholder="请输入场馆区域名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="场馆区域编号" prop="zoneCode">
              <el-input v-model="form.zoneCode" placeholder="请输入场馆区域编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属场馆" prop="venueId">
              <el-select v-model="form.venueId" placeholder="请选择场馆" clearable>
                <el-option
                    v-for="item in venueOptions"
                    :key="item.venueId"
                    :label="item.venueName"
                    :value="item.venueId"
                />
              </el-select>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="面积(平方米)" prop="zoneSize">
              <el-input v-model="form.zoneSize" placeholder="请输入场馆区域面积" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="建成日期" prop="buildDate">
              <el-date-picker clearable
                              v-model="form.buildDate"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择建成日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in junti_venue_status"
                    :key="dict.value"
                    :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="场地分类" prop="category">
              <el-select v-model="form.category" placeholder="请选择场地分类">
                <el-option
                    v-for="dict in junti_venue_category"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支持训练分类" prop="trainType">
              <el-select v-model="form.trainType" placeholder="请选择支持训练分类">
                <el-option
                    v-for="dict in junti_train_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始时段" prop="startTimeSpan">
              <el-time-select
                  placeholder="请选择开始时段"
                  v-model="form.startTimeSpan"
                  :key="form.startTimeSpan"
                  :picker-options="{}">
              </el-time-select>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时段" prop="endTimeSpan">
              <el-time-select
                  placeholder="请选择结束时段"
                  v-model="form.endTimeSpan"
                  :key="form.endTimeSpan"
                  :min-time="form.startTimeSpan"
                 >
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管理人员" prop="manager">
              <el-input v-model="form.manager" placeholder="请输入管理人员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否功能区域" prop="isFunctional">
              <el-radio-group v-model="form.isFunctional">
                <el-radio
                    v-for="dict in common_yes_no"
                    :key="dict.value"
                    :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="平面图" prop="floorPlan">
              <image-upload v-model="form.floorPlan" :isShowTip="false"  :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 新增场地设备管理对话框 -->
    <DeviceManagement ref="deviceRef" @success="getList"/>
  </div>
</template>

<script setup name="VenueZone">
import { listVenueZone, getVenueZone, delVenueZone, addVenueZone, updateVenueZone } from "@/api/base/venueZone";
import {selectVenue} from "@/api/base/venue.js";
import DeviceManagement from './components/DeviceManagement.vue'
const { proxy } = getCurrentInstance();

const venueZoneList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const venueOptions= ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 'zoneId',
    isAsc: 'desc',
    zoneName: null,
    zoneCode: null,
    status: null,
    category: null,
    trainType: null,
    manager: null,
  },
  rules: {
    venueId: [
      { required: true, message: "场馆ID不能为空", trigger: "blur" }
    ],
    zoneName: [
      { required: true, message: "场馆区域名称不能为空", trigger: "blur" }
    ],
    zoneCode: [
      { required: true, message: "场馆区域编号不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
    category: [
      { required: true, message: "场地分类不能为空", trigger: "change" }
    ],
    trainType: [
      { required: true, message: "支持训练分类不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询场馆区域列表 */
function getList() {
  loading.value = true;
  listVenueZone(queryParams.value).then(response => {
    venueZoneList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询场馆信息列表 */
function getVenueList() {
  selectVenue().then(response => {
    venueOptions.value = response.data;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    zoneId: null,
    venueId: null,
    zoneName: null,
    zoneCode: null,
    zoneSize: null,
    floorPlan: null,
    buildDate: null,
    status: "1",
    category: null,
    trainType: null,
    startTimeSpan: null,
    endTimeSpan: null,
    manager: null,
    phonenumber: null,
    orderNum: 10,
    isFunctional: "1"
  };
  proxy.resetForm("venueZoneRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.zoneId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加场馆区域";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _zoneId = row.zoneId || ids.value
  getVenueZone(_zoneId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改场馆区域";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["venueZoneRef"].validate(valid => {
    if (valid) {
      if (form.value.zoneId != null) {
        updateVenueZone(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVenueZone(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _zoneIds = row.zoneId || ids.value;
  proxy.$modal.confirm('是否确认删除选中的场馆区域数据项？').then(function() {
    return delVenueZone(_zoneIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('base/venueZone/export', {
    ...queryParams.value
  }, `venueZone_${new Date().getTime()}.xlsx`)
}
const deviceRef = ref()
// 处理设备管理按钮点击事件
function handleDeviceManagement(row) {
  deviceRef.value.openPage({
    zoneId: row.zoneId,
    zoneName: row.zoneName,
    zoneCode: row.zoneCode
  }, true)
}

getList();
getVenueList();
</script>
